package com.junzhiit.nengji.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.ChainBO;
import com.junzhiit.nengji.bo.SendPhoneMessageBO;
import com.junzhiit.nengji.bo.WechatConfigBO;
import com.junzhiit.nengji.cms.service.ChainService;
import com.junzhiit.nengji.cms.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SystemUserNotFoundException;
import com.junzhiit.nengji.cms.web.util.CMSCodeStatus;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.cms.web.vo.ChainAddVO;
import com.junzhiit.nengji.cms.web.vo.ChainEditVO;
import com.junzhiit.nengji.cms.web.vo.ChainVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.ShopVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.WechatConfigVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.ChainInfoDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.ShopsLocatedDAO;
import com.junzhiit.nengji.dao.SystemUserDAO;
import com.junzhiit.nengji.dao.WechatConfigDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.ChainInfo;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.ShopsLocated;
import com.junzhiit.nengji.model.SystemUser;
import com.junzhiit.nengji.model.WechatConfig;
import com.junzhiit.nengji.model.enums.AuditStatus;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.RegistStatus;

@Service
public class ChainServiceImpl extends BaseServiceImpl implements ChainService{
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private ChainInfoDAO chainInfoDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private SystemUserDAO systemUserDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private ShopsLocatedDAO shopsLocatedDAO;
	
	@Autowired
	private WechatConfigDAO wechatConfigDAO;
	
	@Override
	public ResultVO add(ChainAddVO chainAddVO) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(chainAddVO.getSessionId()), SystemUserSessionVO.class);
		SystemUser systemUser = systemUserDAO.query(systemUserSessionVO.getSystemUserId());
		if (systemUser == null) {
			throw new SystemUserNotFoundException("找不到用户");
		}
		
		//insert chain
		Chain chain=new Chain();
		BeanUtils.copyProperties(chainAddVO, chain);
		chain.setSystemUserId(systemUser.getId());
		int count=chainDAO.getCount(chain);
		if(count>0){
			throw new ChainNotFoundException("已存在连锁信息");
		}
		
		Chain data=chainDAO.insert(chain);
		
		//insert chain_info
		ChainInfo chainInfo=new ChainInfo();
		chainInfo.setChainId(data.getId());
		chainInfoDAO.insert(chainInfo);
		
		//update system_user
		systemUser.setRegiestStatus(RegistStatus.SHOP.toIntStatus());
		systemUser.setIsImproveChain(Is.YES.toIntValue());
		systemUser.setSystemId(data.getId());
		systemUserDAO.update(systemUser);
		
		result.setData(data);
		return result; 
	}
	
	@Override
	public ResultVO edit(ChainEditVO chainEditVO){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(chainEditVO.getSessionId()),SystemUserSessionVO.class);
		SystemUser systemUser=systemUserDAO.query(systemUserSessionVO.getSystemUserId());
		if (systemUser == null) {
			throw new SystemUserNotFoundException("找不到用户");
		}
		Chain paramChain=new Chain();
		paramChain.setSystemUserId(systemUser.getId());
		Chain updateChain=chainDAO.getChain(paramChain);
		if(updateChain==null){
			throw new ChainNotFoundException("连锁品牌未找到");
		}
		BeanUtils.copyProperties(chainEditVO, updateChain,Constants.BASE_FIELD);
		updateChain.setExpireTime(DateUtil.parseDate(chainEditVO.getExpireTime()));
		chainDAO.update(updateChain);
		
		systemUser.setIsAuth(Is.YES.toIntValue());
		systemUser.setRegiestStatus(RegistStatus.INDUSTRY.toIntStatus());
		systemUser.setIsImproveMeal(Is.YES.toIntValue());
		systemUserDAO.update(systemUser);
		
		return result;
	}

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,  ChainVO chainVO) {
		ChainBO chainBO = new ChainBO();
		BeanUtils.copyProperties(chainVO, chainBO);
		List<ChainBO> list = chainDAO.findAll(chainBO, dtp.getStart(), dtp.getLength());
		int totalCount = chainDAO.getTotalCount(chainBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO chainAudit(ChainVO chainVO) {
		ResultVO result = new ResultVO();
		ChainBO cb=new ChainBO();
		BeanUtils.copyProperties(chainVO, cb);
		Chain chain = chainDAO.query(cb.getId());
		if(chain == null){
			throw new ChainNotFoundException("店铺不存在");
		}
		chain.setAuditStatus(cb.getAuditStatus());
		String smsModel=null;
		SendPhoneMessageBO spmb=new SendPhoneMessageBO();
		if(chain.getAuditStatus() == AuditStatus.AUDITFAIL.toIntStatus()){
			chain.setAuditFailDescription(cb.getAuditFailDescription());
			smsModel=SmsUtil.SHOPS_MERCHANTS_AUDIT_FAIL;
			spmb.setAuditFailDescription(cb.getAuditFailDescription());
		}
		if(chain.getAuditStatus() == AuditStatus.AUDITSUCCESS.toIntStatus()){
			chain.setIsExpire(Is.NO.toIntValue());
			chain.setIsRemind(Is.NO.toIntValue());
			chain.setIsRemindExpire(Is.NO.toIntValue());
			chain.setExpireTime(new Date());
			smsModel=SmsUtil.SHOPS_MERCHANTS_AUDIT_SUCCESS;
		}
		chainDAO.update(chain);
		String mobilePhone=chain.getMobilePhone();
		spmb.setMobilePhone(mobilePhone);
		spmb.setSmsModel(smsModel);
		SmsUtil.sendPhoneMessage(spmb);
		return result;
	}

	@Override
	public ResultVO findById(String chainId) {
		ResultVO result = new ResultVO();

		Chain data = chainDAO.query(chainId);
		if (data == null) {
			throw new ShopNotFoundException("品牌不存在");
		}
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO update(ChainVO chainVO) {
		ResultVO result = new ResultVO();
		Chain chain = chainDAO.query(chainVO.getId());
		if(chain == null){
			throw new ChainNotFoundException("找不到连锁品牌");
		}
		BeanUtils.copyProperties(chainVO, chain);
		chain.setExpireTime(DateUtil.parseDate(chainVO.getExpireTime()));
		chainDAO.update(chain);
		return result;
	}
	
	@Override
	public ResultVO merchantsAudit(ShopVO shopVO) {
		ResultVO result = new ResultVO();
		Shop shop = shopDAO.query(shopVO.getId());
		ShopsLocated shopsLocated = shopsLocatedDAO.findByShopId(shopVO.getId());
		if(shop == null){
			throw new ShopNotFoundException("店铺不存在");
		}
		shop.setAuditStatus(shopVO.getAuditStatus());
		if(shop.getAuditStatus() == AuditStatus.AUDITFAIL.toIntStatus()){
			shop.setAuditFailDescription(shopVO.getAuditFailDescription());
		}
		if(shop.getAuditStatus() == AuditStatus.AUDITSUCCESS.toIntStatus()){
			shop.setIsExpire(Is.NO.toIntValue());
			shop.setIsRemind(Is.NO.toIntValue());
			shop.setIsRemindExpire(Is.NO.toIntValue());
			if(shopsLocated != null){
				shopsLocated.setRegistStatus(RegistStatus.AUDITSUCCESS.toIntStatus());
				shopsLocatedDAO.update(shopsLocated);
			}
		}
		shopDAO.update(shop);
		return result;
	}

	@Override
	public ResultVO findBySessionId(String sessionId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),SystemUserSessionVO.class);
		String chainId=systemUserSessionVO.getSystemId();
		Chain chain=chainDAO.query(chainId);
		if(chain==null){
			throw new ChainNotFoundException("品牌不存在");
		}
		result.setData(chain);
		return result;
	}

	@Override
	public ResultVO getChainWechatConfig(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		WechatConfigBO wechatConfigBO = wechatConfigDAO.getWechatConfigByShopId(systemUserSessionVO.getSystemId());
		WechatConfigVO wechatConfigVO = new WechatConfigVO();
		BeanUtils.copyProperties(wechatConfigBO, wechatConfigVO);
		result.setData(wechatConfigVO);
		return result;
	}

	@Override
	public ResultVO addOrUpdateWechatConfig(WechatConfig wechatConfig, String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		try {
			if (StringUtils.isBlank(wechatConfig.getId())) {
				wechatConfig.setCsId(systemUserSessionVO.getSystemId());
				wechatConfigDAO.insert(wechatConfig);
			} else {
				WechatConfig params = wechatConfigDAO.query(wechatConfig.getId());
				BeanUtils.copyProperties(wechatConfig, params);
				wechatConfigDAO.update(params);
			}
		} catch (Exception e) {
			result.setStatus(CMSCodeStatus.FAILED);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@Override
	public ResultVO getChainList(Chain chain) {
		ResultVO result = new ResultVO();
		List<Chain> list = chainDAO.getChainList(chain);
		result.setData(list);
		return result;
	}
}
